package vip.liux.backend.application.service.files;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import vip.liux.backend.application.service.files.dtos.DirectoryDescriptorDto;
import vip.liux.backend.application.service.files.dtos.FileDescriptorDto;
import vip.liux.contracts.infrastructure.automap.MapMapper;
import vip.liux.contracts.infrastructure.automap.MapStructConfig;
import vip.liux.contracts.models.files.DirectoryDescriptor;
import vip.liux.contracts.models.files.FileDescriptor;
import vip.liux.contracts.shared.kits.StorageKit;

import java.util.List;
import java.util.Objects;

@Mapper(config = MapStructConfig.class, uses = {MapMapper.class})
public interface AutoMapperProfile {

    AutoMapperProfile INSTANCE = Mappers.getMapper(AutoMapperProfile.class);

    DirectoryDescriptorDto toDirectoryDescriptorDto(DirectoryDescriptor source);

    @Mapping(target = "cdnUrl", expression = "java(getCdnUrl(source))")
    FileDescriptorDto toFileDescriptorDto(FileDescriptor source);

    List<FileDescriptorDto> toFileDescriptorDtoList(List<FileDescriptor> list);

    default String getCdnUrl(FileDescriptor source) {
        return StorageKit.getCdnUrl(Objects.requireNonNull(source.getId()).toString());
    }
}
